package net.tngou.c4j.thesaurus.parser;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;


import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.io.FileUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class SogouScel {
	private final static Logger log = LoggerFactory.getLogger(SogouScel.class);
	public static void main(String[] args) {
		
	}
	public static Map<String,Integer> GetMap() {
		Map<String, Integer> map = GetWord();
		Map<String, Integer> mapfilter = GetWordFilter();
		map.putAll(mapfilter);
		return map;
				
	}
	
	public static Map<String,Integer> GetWordFilter() {
		Map<String,Integer> rs= new HashMap<>();
		Parameters params = new Parameters();
		@SuppressWarnings({ "rawtypes", "unchecked" })
		FileBasedConfigurationBuilder<?> builder =
			    new FileBasedConfigurationBuilder(FileBasedConfiguration.class)
			    .configure(params.properties()
			        .setFileName("scel/wordfilter.tg"));
		URL url = builder.getFileHandler().getURL();
		try {
			File file = new File(url.toURI());
	
		    List<String> list = FileUtils.readLines(file);
			 
		     int size = list.size();  
		     for(int i = 0; i < size; i++)
		     {  
		          String word = list.get(i);           
		          rs.put(word, -word.length());
		     }  
		       		
		} catch (URISyntaxException | IOException e) {	
			log.error("异常:{}",e);
		}
		return rs;
	}
	
	public static Map<String,Integer> GetWord() {
		Map<String,Integer> rs= new HashMap<>();
		Parameters params = new Parameters();
		@SuppressWarnings({ "rawtypes", "unchecked" })
		FileBasedConfigurationBuilder<?> builder =
			    new FileBasedConfigurationBuilder(FileBasedConfiguration.class)
			    .configure(params.properties()
			        .setFileName("scel"));
		URL url = builder.getFileHandler().getURL();
		try {
			File file = new File(url.toURI());
			Collection<File> listfile = FileUtils.listFiles(file, new String[] {"scel"}, true);
			for (File e : listfile) {
		
			SougouScelMdel model = new SougouScelReader().read(e);
			Map<String,List<String>> words = model.getWordMap(); //词<拼音,词>  
			Set<Entry<String,List<String>>> set = words.entrySet(); 
			Iterator<Entry<String,List<String>>> iter = set.iterator();  
		       while(iter.hasNext())
		       {  
		           Entry<String,List<String>> entry = iter.next();  
		           List<String> list = entry.getValue();  
		           int size = list.size();  
		           for(int i = 0; i < size; i++)
		           {  
		               String word = list.get(i);           
		               rs.put(word, word.length());
		           }  
		       } 		
			};
		} catch (URISyntaxException | IOException e) {	
			log.error("异常:{}",e);
		}
		return rs;
	}
}
